package testChapter7


import org.apache.spark.rdd.JdbcRDD
import org.apache.spark.{SPARK_BRANCH, SparkConf, SparkContext}

import java.sql.DriverManager

object Demo2 {
  def main(args: Array[String]): Unit = {
    val isLocal = args(0).toBoolean //本地运行,在edit conguration
    //sortBy
    val conf = new SparkConf().setAppName("helloSpark")
    if(isLocal){
      conf.setMaster("local[*]")
    }
    val sc = new SparkContext(conf)

    val conn = ()=>DriverManager.getConnection("jdbc:mysql://123.56.187.176:1101/huelkq11","niit","123456")
    //java rdd 应用conn
    val result = new JdbcRDD(
      sc,
      conn,
      "select * from que where id > ? and id < ?",
      1,
      500,
      1,//本地计算1个分区即可
      x=>{
        val id =x.getInt(1)
        val name = x.getString(2)
        (id,name)
      }

    )
    println(result.collect().toBuffer)


  }

}
